package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.PickEntity;
import com.shaoshaohuo.app.utils.FiveCornerStar;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/6/6.
 */

public class Home_Pick_Adapter extends BaseAdapter {
    private Context context;
    private List<PickEntity.DataBean> fragment_data;

    public Home_Pick_Adapter(Context context, List<PickEntity.DataBean> fragment_data) {
        this.context = context;
        this.fragment_data = fragment_data;
    }

    public void setdate(List<PickEntity.DataBean> fragment_data) {
        this.fragment_data = fragment_data;
        notifyDataSetChanged();
    }

    public void adddate(List<PickEntity.DataBean> fragment_data) {
        this.fragment_data.addAll(fragment_data);
        notifyDataSetChanged();
    }

    public List<PickEntity.DataBean> getdate() {
        return fragment_data;
    }


    @Override
    public int getCount() {
        return fragment_data != null ? fragment_data.size() : 0;
    }

    @Override
    public PickEntity.DataBean getItem(int position) {
        return fragment_data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Home_Pick_Adapter.FragmentViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_pick_item, parent, false);
            holder = new FragmentViewHolder();
            holder.pick_address = (TextView) convertView.findViewById(R.id.pick_address);
            holder.pick_distance = (TextView) convertView.findViewById(R.id.pick_distance);
            holder.pick_name = (TextView) convertView.findViewById(R.id.pick_name);
            holder.pick_picture = (ImageView) convertView.findViewById(R.id.pick_picture);
            holder.pick_rating = (FiveCornerStar) convertView.findViewById(R.id.pick_rating);
            holder.pick_people_price = (TextView) convertView.findViewById(R.id.pick_people_price);
            holder.pick_child_price = (TextView) convertView.findViewById(R.id.pick_child_price);
            holder.pick_share_price = (TextView) convertView.findViewById(R.id.pick_share_price);
            holder.pick_saled = (TextView) convertView.findViewById(R.id.pick_saled);
            holder.pick_rating.setNum(5, BitmapFactory.decodeResource(context.getResources(), R.mipmap.star).getWidth(), BitmapFactory.decodeResource(context.getResources(), R.mipmap.star).getHeight());
            convertView.setTag(holder);
        } else {
            holder = (FragmentViewHolder) convertView.getTag();
        }
        holder.pick_name.setText(fragment_data.get(position).getName());
        holder.pick_address.setText(fragment_data.get(position).getAddress());
        Picasso.with(context).load(fragment_data.get(position).getImg()).placeholder(R.mipmap.zhanzhanzhan).into(holder.pick_picture);
        holder.pick_distance.setText("距" + fragment_data.get(position).getDistance() + "km");
        if (fragment_data.get(position).getBuyNum() != null) {
            holder.pick_saled.setText("已售:" + fragment_data.get(position).getBuyNum());
        }
        holder.pick_rating.setIsdown(false);

        holder.pick_rating.setdownnum(fragment_data.get(position).getGoodLevel());
        holder.pick_child_price.setText("￥" + fragment_data.get(position).getChild() + "/人");
        holder.pick_people_price.setText("￥" + fragment_data.get(position).getAdult() + "/人");
        holder.pick_share_price.setText("￥" + fragment_data.get(position).getTeam() + "/人");
        return convertView;
    }


    class FragmentViewHolder {
        TextView pick_distance;
        FiveCornerStar pick_rating;
        TextView pick_address;
        TextView pick_name;
        ImageView pick_picture;
        TextView pick_people_price, pick_child_price, pick_share_price;
        TextView pick_saled;
    }

}
